package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hpplay.sdk.source.api.LelinkPlayerInfo;
import com.hpplay.sdk.source.api.LelinkSourceSDK;
import com.stark.cast.screen.CastScreenManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ProImgAdapter;
import flc.ast.bean.ProImgBean;
import flc.ast.databinding.ActivityProPictureBinding;
import stark.common.basic.utils.UriUtil;

public class ProPictureActivity extends BaseAc<ActivityProPictureBinding> {

    public static List<String> listPath = new ArrayList<>();
    private CastScreenManager mCastScreenManager;
    private int oldPosition = 0;
    private ProImgAdapter imgAdapter;


    @Override
    protected int onCreate() {
        return R.layout.activity_pro_picture;
    }

    @Override
    protected void initView() {
        mDataBinding.ivProImgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        mCastScreenManager = CastScreenManager.getInstance();
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        imgAdapter.getItem(oldPosition).setSelect(false);
        imgAdapter.notifyItemChanged(oldPosition);
        oldPosition=position;
        imgAdapter.getItem(position).setSelect(true);
        imgAdapter.notifyItemChanged(position);
        Glide.with(ProPictureActivity.this).load(imgAdapter.getItem(position).getPath()).into(mDataBinding.ivProImgSel);
        proPicture(imgAdapter.getItem(position).getPath());
    }

    @Override
    protected void initData() {
        if (listPath.size() == 0) {
            return;
        }
        List<ProImgBean> list = new ArrayList<>();
        for (String path : listPath) {
            list.add(new ProImgBean(path, false));
        }
        mDataBinding.rvProImgList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        imgAdapter = new ProImgAdapter();
        mDataBinding.rvProImgList.setAdapter(imgAdapter);
        imgAdapter.setList(list);
        imgAdapter.setOnItemClickListener(this);
        Glide.with(this).load(list.get(0).getPath()).into(mDataBinding.ivProImgSel);
        proPicture(list.get(0).getPath());
    }

    //图片投屏
    private void proPicture(String url) {
        LelinkPlayerInfo playerInfo = new LelinkPlayerInfo();
        playerInfo.setType(LelinkSourceSDK.MEDIA_TYPE_IMAGE);
        Uri uri = Uri.parse(url);
        if (UriUtil.isContentSchemeUri(uri)) {
            playerInfo.setLocalUri(uri);
        } else {
            playerInfo.setLocalPath(url);
        }
        mCastScreenManager.startPlay(playerInfo);
    }

}